let commonUtil:CommonUtil.Util= {} as CommonUtil.Util ;

/**
 * @method isType 判断对象类型
 * @param obj {Object} 需要判断的对象
 * @param type {String} 类型字符串
 * @returns {Boolean}
 * */
commonUtil.isType = (obj: any, type: string): boolean => {
    return Object.prototype.toString.call(obj) === '[object ' + type + ']';
};


/**
 * @method isArray 判断该对象是否是数组
 * @param obj {Object} 需要判断的对象
 * @returns {Boolean}
 * */
commonUtil.isArray = (obj: any): boolean => {
    return commonUtil.isType(obj, 'Array');
};

/**
 * @method isStr 判断该对象是否是字符串
 * @param obj {Object} 需要判断的对象
 * @returns {Boolean}
 * */
commonUtil.isStr = (obj: any): boolean => {
    return commonUtil.isType(obj, 'String');
};

/**
 * @method isObject 判断该对象是否是Object
 * @param obj {Object} 需要判断的对象
 * @returns {Boolean}
 * */
commonUtil.isObject = (obj: any): boolean => {
    return commonUtil.isType(obj, 'Object');
};

/**
 * @method isFun 判断该对象是否是函数
 * @param obj {Object} 需要判断的对象
 * @returns {Boolean}
 * */
commonUtil.isFun = (obj: any): boolean => {
    return commonUtil.isType(obj, 'Function');
};

/**
 * @method isNull 判断该对象是否是bull
 * @param obj {Object} 需要判断的对象
 * @returns {Boolean}
 * */
commonUtil.isNull = (obj: any): boolean => {
    return commonUtil.isType(obj, 'Null');
};

/**
 * @method isJson 判断该对象是否是json
 * @param obj {Object} 需要判断的对象
 * @returns {Boolean}
 * */
commonUtil.isJson = (obj: any): boolean => {
    return commonUtil.isObject(obj) && !commonUtil.isNull(obj) && !commonUtil.isArray(obj);
};

/**
 * @method isJson 判断该对象是否是json
 * @param str {Object} 需要判断的对象
 * @returns {string | Record<string, any>}
 * */
commonUtil.isJsonStr = (str: string): string | Record<string, any> => {
    try {
        return JSON.parse(str);
    } catch (error) {
        return str;
    }
};

export default commonUtil;